package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.SensitiveMapper;
import com.heima.admin.service.SensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.dtos.SensitivePageDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;

@Service
@Slf4j
@Transactional
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, WmSensitive> implements SensitiveService {
    @Override
    public ResponseResult create(SensitiveDto sensitiveDto) throws InvocationTargetException, IllegalAccessException {
        if (sensitiveDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(sensitiveDto, wmSensitive);
        save(wmSensitive);
        return ResponseResult.okResult(wmSensitive);
    }

    @Override
    public ResponseResult del(Integer id) {
        return null;
    }

    @Override
    public ResponseResult findlist(SensitivePageDto sensitivePageDto) {
        sensitivePageDto.checkParam();
        IPage page = new Page(sensitivePageDto.getPage(), sensitivePageDto.getSize());
        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (sensitivePageDto.getStatus()!=null){
            lambdaQueryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        }
       if (StringUtils.isNotBlank(sensitivePageDto.getName())){
           lambdaQueryWrapper.like(WmSensitive::getSensitives,sensitivePageDto.getName());
       }
       page=page(page,lambdaQueryWrapper);
        ResponseResult responseResult = new PageResponseResult(sensitivePageDto.getPage(), sensitivePageDto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
return responseResult;
    }


}
